package com.luck.easyfoodbatch.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.mvp.model.Batches;
import com.luck.easyfoodbatch.util.Tools;

import java.util.ArrayList;
import java.util.List;

/**
 * author：luck
 * project：EasyFoodBatch
 * package：com.luck.easyfoodbatch.adapter
 * email：893855882@qq.com
 * data：16/9/29
 */
public class UpdateBatchItemAdapter extends RecyclerView.Adapter<UpdateBatchItemAdapter.ItemViewHolder> {
    private List<Batches.DataBean> list = new ArrayList<>();
    private Context mContext;
    protected OnItemClickListener mItemClickListener;
    private String unit;
    private int num;

    public String getUnit() {
        return unit;
    }

    public void setUnit(String unit) {
        this.unit = unit;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mItemClickListener = listener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position, View v);
    }

    public OnRemoveBatch removeBatch;

    public interface OnRemoveBatch {
        void onRemoveBatch(int position, Batches.DataBean bean);
    }

    public UpdateBatchItemAdapter(Context mContext, List<Batches.DataBean> list, OnRemoveBatch removeBatch, int num) {
        super();
        this.list = list;
        this.mContext = mContext;
        this.removeBatch = removeBatch;
        this.num = num;
    }


    @Override
    public UpdateBatchItemAdapter.ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View rootView = LayoutInflater.from(parent.getContext()).inflate(R.layout.batch_list_item, parent, false);
        return new ItemViewHolder(rootView);
    }

    public void setList(List<Batches.DataBean> list) {
        this.list = list;
    }

    @Override
    public void onBindViewHolder(final UpdateBatchItemAdapter.ItemViewHolder holder, final int position) {
        final Batches.DataBean bean = list.get(position);
        holder.iv_remove.setTag(position);// 防止快速滑动下标错乱
        int tag = (int) holder.iv_remove.getTag();
        if (tag == 0) {
            holder.iv_remove.setVisibility(View.GONE);
        } else {
            holder.iv_remove.setVisibility(View.VISIBLE);
        }
        String province = bean.getSold_province();// 城市id
        String price = bean.getPrice();
        String province_zh = bean.getSold_province_zh();
        String uit = bean.getUnit();
        String single_price = bean.getSingle_price();
        holder.tv_unit_price.setText(single_price);

        if (!Tools.isNull(uit)) {
            holder.tv_price.setText(price);
        } else {
            if (Tools.isNull(unit)) {
                unit = "";
            }
            holder.tv_price.setText(price);
        }
        holder.tv_area.setText(province_zh);
        holder.iv_remove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                removeBatch.onRemoveBatch(position, bean);
            }
        });

        //itemView 的点击事件
        if (mItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mItemClickListener.onItemClick(holder.getAdapterPosition(), v);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder {
        TextView tv_area, tv_price,tv_unit_price;
        ImageView iv_remove;

        public ItemViewHolder(View view) {
            super(view);
            tv_price = (TextView) view.findViewById(R.id.tv_price);
            tv_area = (TextView) view.findViewById(R.id.tv_area);
            iv_remove = (ImageView) view.findViewById(R.id.iv_remove);
            tv_unit_price = (TextView) view.findViewById(R.id.tv_unit_price);
        }
    }

}
